package com.example.lancooznbk.view.adapter;

import android.animation.Animator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import com.example.lancooznbk.model.result.BaseReviewItemEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import com.example.lancooznbk.view.widget.RatingBar;
import com.example.lancooznbk.view.widget.RippleView;
import com.lancoo.reviewsystem.utils.TimeUtils;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by tangminglong on 17/7/25.
 */

public class BaseReviewAdapter extends BaseRecyclerAdapter<BaseReviewAdapter.ViewHolder>{


    private Context mContext;
    private List<BaseReviewItemEntity.BaseReviewItemBean> mList;
    private int mLastPosition = -1;
    private OnItemClickListener onItemClickListener;

    public BaseReviewAdapter(Context context) {
        this.mContext = context;
        mList = new ArrayList<>();
    }

    public BaseReviewAdapter(Context mContext, List<BaseReviewItemEntity.BaseReviewItemBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public  interface OnItemClickListener{
        void onItemClick(View view, List<BaseReviewItemEntity.BaseReviewItemBean> list,int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view,false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_base_review, parent,false);
        return new ViewHolder(view,true);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,  int position, boolean isItem) {
        holder.rippleLayout.setTag(position);
        //设置波纹效果
        holder.rippleLayout.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {
                if ( mList.size() != 0) {
                    if (onItemClickListener != null){
                        onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
                    }
                }

            }
        });

      /*  holder.paperInto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( mList.size() != 0) {
                   if (onItemClickListener != null){
                       onItemClickListener.onItemClick(v,mList, (Integer) v.getTag());
                   }
                }
            }
        });*/

        BaseReviewItemEntity.BaseReviewItemBean itemBean = mList.get(position);

        Date date = itemBean.getLastAnsweredTime();
        String answerTime = TimeUtils.getDateType2(date);
        if (itemBean.getState() == 2){//已经提交
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.ic_into_result);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("查看结果");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getState() == 1){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.ic_into_answer);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("继续作答");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
        }else if (itemBean.getState() == 0){
            Drawable drawRight = ContextCompat.getDrawable(mContext,R.mipmap.ic_into_answer);
            drawRight.setBounds(0,0,drawRight.getIntrinsicWidth(),drawRight.getIntrinsicHeight());
            holder.tvIntoDetail.setText("开始作答");
            holder.tvIntoDetail.setCompoundDrawables(null,null,drawRight,null);
            answerTime = mContext.getString(R.string.no_answered);
        }
        String lastAnswerTime = String.format(mContext.getString(R.string.paper_lastAnswerTime),answerTime);
        int finishedCount = itemBean.getAnsweredItemCount();
        int totalCount = itemBean.getTotalItemCount();
        String paperTotal = String.format(mContext.getString(R.string.paper_itemTotal),totalCount);
        String paperFinishedTotal = String.format(mContext.getString(R.string.paper_itemFinishedTotal),finishedCount);


        holder.paperInduction.setText(itemBean.getTestPaperName());
        holder.paperTotal.setText(Html.fromHtml(paperTotal));
        holder.paperFinishedTotal.setText(Html.fromHtml(paperFinishedTotal));
        holder.paperLastAnswerTime.setText(lastAnswerTime);
//        holder.ratingBar.setStar(itemBean.getDifficulty());

//        float finishedPercent = (float) Math.floor((float)finishedCount / totalCount * 100) / 100;
//        NumberFormat format = NumberFormat.getPercentInstance();// 获取格式化类实例 带百分比
//        format.setMinimumFractionDigits(0);// 设置小数位
//        holder.paperFinishPercent.setText("作答"+ format.format(finishedPercent));
//        holder.progressBar.setProgress((int)(finishedPercent*100));


        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
           anim.start();
        }
        mLastPosition = adapterPosition;

    }

    @Override
    public int getAdapterItemCount() {
        return mList.size();
    }

    public BaseReviewItemEntity.BaseReviewItemBean getItem(int position) {
        return mList.get(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView paperLastAnswerTime;
        private  TextView paperFinishedTotal;
        private RippleView rippleLayout;
        private TextView paperInduction;//资料简介
        private TextView paperTotal;//资料试题数量
//        private TextView paperFinishPercent;//资料完成度
//        private ProgressBar progressBar;//资料进度显示
        private LinearLayout paperInto;
//        private RatingBar ratingBar;
        private TextView tvIntoDetail;

        public ViewHolder(View itemView,boolean isItem) {
             super(itemView);
             if (isItem){
                 //波纹效果
                 rippleLayout = (RippleView) itemView.findViewById(R.id.item_ripper);
                 paperInduction = (TextView) itemView.findViewById(R.id.tv_induction);
                 paperLastAnswerTime = (TextView) itemView.findViewById(R.id.tv_lastTime);
                 paperTotal = (TextView) itemView.findViewById(R.id.tv_total);
                 paperFinishedTotal = (TextView) itemView.findViewById(R.id.tv_answered);
//                 paperFinishPercent = (TextView) itemView.findViewById(R.id.tv_finishedPercent);
//                 progressBar = (ProgressBar) itemView.findViewById(R.id.pb_finished);
                 paperInto = (LinearLayout) itemView.findViewById(R.id.liner_into);
//                 ratingBar = (RatingBar) itemView.findViewById(R.id.rb_hval);
                 tvIntoDetail = (TextView) itemView.findViewById(R.id.tv_into_detail);
             }
         }
     }


    public void clearAndAdd(List<BaseReviewItemEntity.BaseReviewItemBean> data){
        mLastPosition = -1;
        mList.clear();
        mList.addAll(data);
        notifyDataSetChanged();
    }
    public void add(List<BaseReviewItemEntity.BaseReviewItemBean> data){
        int startPosition = getStart()+getAdapterItemCount();
        mList.addAll(data);
        notifyItemRangeInserted(startPosition,data.size());

    }
}
